package spynx.desl.runtime.operators.impl;

import spynx.desl.runtime.IExecutionContext;
import spynx.desl.runtime.IOperator;

public class AddOperator implements IOperator {

	@Override
	public String getOperator() {
		return "+";
	}

	@Override
	public Object apply(IExecutionContext context, String operator, Object... args) {
		Object op1 = context.resolveValue(args[0]);
		Object op2 = context.resolveValue(args[1]);
		if (op1 instanceof Integer) {
			if (op2 instanceof Integer)
				return (Integer)op1 + (Integer)op2;
			else
				return (Integer)op1 + Integer.parseInt(op2.toString());
		}
		if (op1 instanceof String) {
			return (String)op1 + op2.toString();
		}
		throw new RuntimeException(String.format("Operator + is not applicable between type %s and %s",
				op1.getClass().getName(), op2.getClass().getName()));
	}

}
